<template>
  <el-dialog :title="title" :visible.sync="dialogVisible" width="600px" :close-on-click-modal="false" @close="close">
    <el-form ref="form" v-loading="loading" :model="form" :rules="rules" label-width="100px">

      <el-form-item label="角色名称：" prop="name">
        <el-input v-model="form.name" type="name" placeholder="请输入角色名称" />
      </el-form-item>

      <el-form-item label="备注：" prop="description">
        <el-input v-model="form.description" type="textarea" maxlength="30" placeholder="说明备注" />
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button size="small" round @click="hide">取 消</el-button>
      <el-button size="small" round type="primary" @click="handleSubmit('form')">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { validateStrLen } from '@/utils/validate.js'
import { addRole, getRole, updateRole } from '@/api/role'

export default {
  props: {
    scope: {
      require: true,
      type: Number
    }
  },

  data() {
    const validateName = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入角色名称'))
      } else if (!validateStrLen(value, 30)) {
        callback(new Error('长度不超过15个汉字或30个字符'))
      } else {
        callback()
      }
    }
    return {
      rules: {
        name: [
          {
            required: true,
            validator: validateName,
            trigger: ['blur']
          }
        ]
      },
      resolve: null,
      reject: null,
      roleId: null,
      loading: false,
      dialogVisible: null,
      title: null,
      form: {
        name: '',
        type: 0,
        description: ''
      }
    }
  },

  methods: {
    fetchData() {
      this.loading = true
      return getRole(this.roleId, this.scope)
        .then(res => {
          this.loading = false
          if (res.code === 0) {
            this.form = res.data
          } else {
            this.$message.error(res.msg)
          }
        })
        .catch(() => {
          this.loading = false
        })
    },

    openAdd() {
      this.mode = 'add'
      this.title = '新增角色'
      this.roleId = null
      this.show()
      return new Promise((resolve, reject) => {
        this.resolve = resolve
        this.reject = reject
      })
    },

    openEdit(roleId) {
      this.title = '编辑角色'
      this.mode = 'edit'
      this.roleId = roleId
      this.show()
      this.fetchData()
      return new Promise((resolve, reject) => {
        this.resolve = resolve
        this.reject = reject
      })
    },

    show() {
      this.dialogVisible = true
    },

    close() {
      this.loading = false
      this.form = {
        name: '',
        type: 0,
        description: ''
      }
      this.$refs.form.resetFields()
    },

    hide() {
      this.resolve = null
      this.reject = null
      this.dialogVisible = false
    },

    handleParentIdChange(value) {
      if (value && value.length > 0) {
        this.form.parentId = value[value.length - 1]
      } else {
        this.form.parentId = -1
      }
    },

    updateObject() {
      this.loading = true
      return updateRole(this.roleId, this.form, this.scope)
        .then(res => {
          this.loading = false
          if (res.code === 0) {
            this.$notify({
              title: '成功',
              message: '更新成功',
              type: 'success',
              duration: 2000,
              offset: 40
            })
            this.resolve && this.resolve()
            this.hide()
          } else {
            this.$message.error(res.msg)
          }
        })
        .catch(() => {
          this.loading = false
        })
    },

    addObject() {
      if (!this.form.parentId) {
        this.form.parentId = -1
      }
      this.loading = true
      return addRole(this.form, this.scope)
        .then(res => {
          this.loading = false
          if (res.code === 0) {
            this.$notify({
              title: '成功',
              message: '添加成功',
              type: 'success',
              duration: 2000,
              offset: 40
            })
            this.resolve && this.resolve()
            this.hide()
          } else {
            this.$message.error(res.msg)
          }
        })
        .catch(() => {
          this.loading = false
        })
    },

    handleSubmit(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          if (this.mode === 'edit') {
            this.updateObject()
          } else {
            this.addObject()
          }
        }
      })
    }
  }
}
</script>
